/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package SteamEngineFX.Layouts;

import SteamEngineDB.Schedule;
import SteamEngineDB.ScheduleRun;
import SteamEngineFX.SteamEngineFX;
import SteamEngineFX.util.RunTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;

/**
 *
 * @author jbendixsen
 */
public class NavigationPane extends VBox{

    private ApplicationPane appPane = null;
    
    private ToolBar toolbar = null;
    
    private VBox runBox = null;
    
    private List<RunPane> runs = new ArrayList<RunPane>();
    
    private static final Image DateTime24 = new Image(SteamEngineFX.class.getResourceAsStream("/steamenginefx/resources/date_time24.png"));
    //private static final Image BulletBall16 = new Image(SteamEngineFX.class.getResourceAsStream("/steamenginefx/resources/bullet_ball_grey16.png"));
    
    public NavigationPane(ApplicationPane AppPane) {
        
        appPane = AppPane;
        
        setPrefWidth(200);
        getStyleClass().add("NavigationPane");
        
        Button btnRefresh = new Button("Refresh");
        btnRefresh.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                LoadSchedule(appPane.getCurrentSchedule());
            }
        });
        
        toolbar = new ToolBar();
        toolbar.getItems().add(btnRefresh);
        
        
        //accordion = new Accordion();
        runBox = new VBox();
        
        //getChildren().addAll(toolbar, accordion);
        getChildren().addAll(toolbar, runBox);
        
    }
    
    //This observable property all work area pane to respond to changes in the run selection
    private ObjectProperty selectedRun = new SimpleObjectProperty();
    public final ScheduleRun getSelectedRun(){return (ScheduleRun)selectedRun.get();}
    public final void setSelectedRun(ScheduleRun run){selectedRun.set(run);}
    
    
    public void LoadSchedule(Schedule schedule){

        runBox.getChildren().clear();
        runs.clear();
        ScheduleRun firstRun = null;
        if (schedule != null){
            for(Iterator<ScheduleRun> i = schedule.getScheduleRunCollection().iterator(); i.hasNext(); ){
                ScheduleRun r = i.next();
                if (runs.add(new RunPane(r))){
                    System.out.printf("Loading run %s at index %d\n", new RunTime(r.getRunTime()).toString(), runs.size());
                    runBox.getChildren().add(runs.get(runs.size()-1));
                }
                if (firstRun == null){
                    firstRun = r; 
                }
            }
        }
        if (firstRun != null){
            setSelectedRun(firstRun);
        }
    }
           
    
}
